package pers.ree.demo.security;

import org.apache.commons.codec.binary.Base64;

import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.Signature;
import java.security.spec.X509EncodedKeySpec;

public class SignatureTest {

    private static String signSha1 = "ioEYZd88P262TW3WgTPgJSg5fF1p799ksptogtS6HUH7iUV/maRS8A90gAWbv8YOB9FaptYMA+BtHY+WvDZqpD/63mJ6ZR6O0cvxQlmUu9HEZd4o3NOBW+6/3uivMCcdkbGzHwxNILmyEfZL+s/LyCeAYcRomI79YDED5u79GvY=";

    private static String content = "content";

    private static String pubKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCcXqJpdAz1jVZ5LnG3cz4e2r/P1EsYwiR1oH+CLIeyz0yzFTB0OmejgdgkwyofVOOv7sudPgJbY8AYjLw18ujTz6wHOO1RXWJKaYdxr8qCEGAPenrRrdp4deIZeTYhuzFE0TQAMyt3u8DNuE7QnS5ZlwCHve6AWy9tkfOcUF4j0wIDAQAB";

    private static String pubKey1 = "pubKey1";

    public static void main(String[] args) {
        new Thread(() -> {
            try {
                boolean ret = false;
                PublicKey key = generatePublicKeyFromString(pubKey);
                Signature signature = Signature.getInstance("SHA1WithRSA");
                System.out.println("t1 System.identityHashCode(signature)="+System.identityHashCode(signature));
                Thread.sleep(1000);
                signature.initVerify(key);
                signature.update(content.getBytes("UTF-8"));
                ret = signature.verify(Base64.decodeBase64(signSha1.getBytes()));
                System.out.println("t1 ret="+ret);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }, "t1").start();

        new Thread(() -> {
            try {
                boolean ret = false;
                PublicKey key = generatePublicKeyFromString(pubKey1);
                Signature signature = Signature.getInstance("SHA1WithRSA");
                System.out.println("t2 System.identityHashCode(signature)="+System.identityHashCode(signature));
                signature.initVerify(key);
                signature.update(content.getBytes("UTF-8"));
                ret = signature.verify(Base64.decodeBase64(signSha1.getBytes()));
                System.out.println("t2 ret="+ret);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }, "t2").start();
    }

    public boolean checkSign(String sign, String content, String publicKey, String charset) {
        boolean ret = false;
        try {
            PublicKey key = generatePublicKeyFromString(publicKey);
            Signature signature = Signature.getInstance("SHA1WithRSA");
            signature.initVerify(key);
            signature.update(content.getBytes(charset));
            ret = signature.verify(Base64.decodeBase64(sign.getBytes()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ret;
    }

    public static PublicKey generatePublicKeyFromString(String publicKey) throws Exception {
        KeyFactory factory = KeyFactory.getInstance("RSA");
        byte[] key = Base64.decodeBase64(publicKey.getBytes());
        X509EncodedKeySpec encodedKey = new X509EncodedKeySpec(key);
        return factory.generatePublic(encodedKey);
    }
}
